function log(e){console.log(e)}function retIsOk(e){return e&&"object"==typeof e&&1==e.status?!0:!1}function getImageSize(e,t){function a(e,a){i.parentNode.removeChild(i),t({width:e,height:a})}var i=document.createElement("img");i.onload=function(){a(i.clientWidth,i.clientHeight)},i.onerror=function(){a()},i.src=e;var s=i.style;s.visibility="hidden",s.position="fixed",s.bottom=s.left=0,s.width=s.height="auto",document.body.appendChild(i)}var o={maxSelected:G.maxSelected,selectedZoneO:$("#preview"),previewO:$("#preview"),selectedImages:[],imageAttrs:{},pageNum:1,pagination:function(e){var t=this;$(".pagination").pagination(e,{items_per_page:G.perPageItems,callback:function(e){t.pageNum=e+1,t.renderImages($("#albumsForList").val(),t.pageNum,!1)}})},showMsg:function(e){$("#msg").html(e).css("display","inline"),setTimeout(function(){$("#msg").fadeOut()},2e3)},pageAddAlbum:function(e){var t='<option value="'+e.id+'">'+e.album_name+"</option>";$("#albumsForUpload").append(t).val(e.id),$("#albumsForList").append(t)},renderAlbums:function(){var e=this;$.get("server/index.php?action=album:getAlbums",function(t){if(t){var a="";for(var i in t){var s=t[i],r='<option value="'+s.album_id+'">'+s.album_name+"</option>";a+=r}$("#albumsForUpload").html(a),$("#albumsForList").html(a);var l=$("#albumsForList").val();e.renderImages(l,1,!0)}})},imageMaskO:$("#imageMask"),noImagesO:$("#noImages"),loadingO:$("#loading"),loadingStart:function(){this.imageMaskO.is(":hidden")&&this.imageMaskO.css("opacity",.8).show(),this.noImagesO.hide(),this.loadingO.show()},loadingEnd:function(){this.imageMaskO.hide()},noImages:function(){this.imageMaskO.show().css("opacity",1),this.noImagesO.show(),this.loadingO.hide()},renderImages:function(e,t,a){var i=this;t||(t=1),i.loadingStart(),$.get("server/index.php?action=file:getImages",{album_id:e,page:t},function(e){if(!e||!e.count)return void i.noImages();i.loadingEnd();var t=e.datas,s={};for(var r in i.selectedImages){var l=i.selectedImages[r];s[l]=!0}var n="";for(var r in t){var o=t[r],d="",l=G.imageSrcPrefix+"/"+o.file_path;s[l]&&(d='class="selected"'),n+="<li "+d+">",n+='<a title="" href="javascript:;" class="a-img"><img alt="" data-original="'+l+'" ></a>',n+='<div class="tools"><a href="javascript:;" class="del" data-id="'+o.file_id+'"><span class="glyphicon glyphicon-trash"></span></a></div>',n+="</li>"}$("#imageList").html(n),a&&i.pagination(e.count),$("#imageList img").lazyload()})},initSelectedZones:function(){var e=this;num=this.maxSelected,e.previewO.html("");for(var t=1;num>=t;++t)e.previewO.append("<li>"+t+"</li>")},reRenderSelectedImages:function(e,t){for(var a=this,i=this.selectedZoneO.find("li"),s=this.selectedImages.length-1,r=0;r<this.maxSelected;++r){var l=i.eq(r);if(r>s)l.html(r+1);else{src=this.selectedImages[r];var n=a.imageAttrs[src],o="";n&&(n.width&&(o+=' data-width="'+n.width+'"'),n.height&&(o+=' data-height="'+n.height+'"'),n.title&&(o+=' data-title="'+n.title+'"')),l.html("<img "+o+' src="'+src+'" width="60"/><div class="tools"><a title="click to remove this image" href="javascript:;" class="del"><span class="glyphicon glyphicon-trash"></span></a></div>')}e?l.removeClass("selected"):t==src&&l.click()}},removeSelectedImage:function(e){var t=this,a=e.find("img").attr("src");for(var i in this.selectedImages)this.selectedImages[i]==a&&this.selectedImages.splice(i,1);this.reRenderSelectedImages(!0),t.clearAttrs()},addSelectedImage:function(e){if(this.maxSelected<=this.selectedImages.length)return!1;if("object"==typeof e)var t=e.find("img").attr("src");else t=e;return this.selectedImages.push(t),this.reRenderSelectedImages(!1,t),!0},initDataFromTinymce:function(){var e=this,t=top.LEAUI_DATAS,a="";if(t&&t.length>0){for(var i in t){var s=t[i];a=s.src,e.selectedImages.push(s.src),e.imageAttrs[s.src]=s}e.reRenderSelectedImages(!1,a)}},init:function(){var e=this;$("#addAlbumBtn").click(function(){var t=$("#albumName").val();return t?void $.get("server/index.php?action=album:addAlbum",{album_name:t},function(a){retIsOk(a)&&($("#albumName").val(""),e.showMsg("Add Sucess!"),a.album_name=t,e.pageAddAlbum(a))}):void $("#albumName").focus()}),$("#deleteAlbumBtn").click(function(){var t=$("#albumsForUpload").val();$.get("server/index.php?action=album:deleteAlbum",{album_id:t},function(a){1==a?(e.showMsg("Delete Sucess!"),$("#albumsForUpload option[value='"+t+"']").remove(),$("#albumsForList").val()==t&&(e.needRefresh=!0),$("#albumsForList option[value='"+t+"']").remove()):alert("This album has images, please delete it's images at first.")})}),$("#albumsForList").change(function(){var t=$(this).val();e.renderImages(t,1,!0)}),$("#imageList").on("click","li",function(){$(this).hasClass("selected")?($(this).removeClass("selected"),e.removeSelectedImage($(this))):e.addSelectedImage($(this))&&$(this).addClass("selected")}),$("#imageList").on("click",".del",function(t){var a=this;if(t.stopPropagation(),confirm("Are you sure to delete this image ?")){var i=$(this).data("id");$.get("server/index.php?action=file:deleteImage",{file_id:i},function(t){if(t){var i=$(a).closest("li");i.hasClass("selected")&&e.removeSelectedImage(i),$(a).closest("li").remove()}})}}),$("#preview").on("click",".del",function(t){t.stopPropagation();var a=$(this).closest("li"),i=a.find("img").attr("src");e.removeSelectedImage(a),$("#imageList img").each(function(){var e=$(this).attr("src");i==e&&$(this).parent().parent().removeClass("selected")})}),$("#goAddImageBtn").click(function(){$("#albumsForUpload").val($("#albumsForList").val()),$("#myTab li:eq(1) a").tab("show")}),$("#myTab a").click(function(t){t.preventDefault(),$(this).tab("show"),e.needRefresh&&"#images"==$(this).attr("href")&&(setTimeout(function(){var t=$("#albumsForList").val();e.renderImages(t,e.pageNum,!0)},200),e.needRefresh=!1)}),$("#refresh").click(function(){var t=$("#albumsForList").val();e.renderImages(t,e.pageNum,!1)}),$("#addImageUrlBtn").click(function(t){t.preventDefault();var a=$.trim($("#imageUrl").val());return a?void getImageSize(a,function(t){return t.width&&t.height?($("#msgForUrl").hide(),$("#imageUrl").val(""),void e.addSelectedImage(a)):void $("#msgForUrl").show()}):void $("#imageUrl").focus()}),$("#preview").on("click","li",function(){$(this).hasClass("selected")||$(this).find("img").length&&($("#preview li").removeClass("selected"),$(this).addClass("selected"),e.initAttr($(this)))}),$("#attrTitle, #attrWidth, #attrHeight").on("blur",function(){e.modifyAttr($(this))}),$("#attrConstrain").on("click",function(){e.modifyAttr($(this))}),e.initSelectedZones(),e.initDataFromTinymce(),e.renderAlbums(),e.initUploader()},curSrc:"",curLi:null,attrTitleO:$("#attrTitle"),attrWidthO:$("#attrWidth"),attrHeightO:$("#attrHeight"),attrConstrainO:$("#attrConstrain"),clearAttrs:function(){var e=this;e.attrTitleO.val("").attr("disabled",!0),e.attrHeightO.val("").attr("disabled",!0),e.attrWidthO.val("").attr("disabled",!0),e.attrConstrainO.prop("checked",!1).attr("disabled",!0)},scale:function(e){var t=this,a=t.attrConstrainO.is(":checked"),i=+t.attrWidthO.val(),s=+t.attrHeightO.val();if(!isNaN(i)&&!isNaN(s)){var r=t.getCurAttrs(),l=r.preWidth||r.width,n=r.preHeight||r.height;a&&l&&n&&(e?(s=parseInt(i/l*n),t.attrHeightO.val(s)):(i=parseInt(s/n*l),t.attrWidthO.val(i)));var o={width:i,height:s};return o}},getCurAttrs:function(){var e=this;return e.imageAttrs[e.curSrc]},setCurDataAttrs:function(e){var t=this,a=t.curLi.find("img");a.attr("data-width",e.width),a.attr("data-height",e.height),a.attr("data-title",e.title),t.imageAttrs[t.curSrc]=e},modifyAttr:function(e){var t=this,a=e.attr("id"),i=e.val(),s=t.getCurAttrs();if(s){switch(a){case"attrConstrain":i=0,e.is(":checked")&&(i=1),s.constrain=i;break;case"attrTitle":s.title=i;break;case"attrWidth":$.extend(s,t.scale(!0));break;case"attrHeight":$.extend(s,t.scale(!1))}t.setCurDataAttrs(s)}},initAttr:function(e){function t(e){e=e||{},a.attrTitleO.val(e.title).attr("disabled",!1),a.attrWidthO.val(e.width).attr("disabled",!1),a.attrHeightO.val(e.height).attr("disabled",!1),a.attrConstrainO.attr("disabled",!1),e.constrain?a.attrConstrainO.prop("checked",!0):a.attrConstrainO.prop("checked",!1),a.setCurDataAttrs(e)}var a=this;"object"!=typeof e&&(e=$("#preview").find('img[src="'+e+'"]').parent());var i=e.find("img").attr("src");a.curSrc=i,a.curLi=e;var s=a.imageAttrs[i];s=s||{},s&&s.width&&s.height?t(s):getImageSize(i,function(e){return e.title=s.title||"",e.constrain=1,e.preWidth=e.width,e.preHeight=e.height,i!=a.curSrc?(a.imageAttrs[i]=e,void a.setCurDataAttrs(s)):void t(e)})},needRefresh:!1,uploadRefreshImageList:function(){var e=this,t=$("#albumsForList").val();t==$("#albumsForUpload").val()&&(e.needRefresh=!0)},initUploader:function(){function e(e){return"number"!=typeof e?"":e>=1e9?(e/1e9).toFixed(2)+" GB":e>=1e6?(e/1e6).toFixed(2)+" MB":(e/1e3).toFixed(2)+" KB"}var t=this,a=$("#upload ul");$("#drop a").click(function(){$(this).parent().find("input").click()}),$("#upload").fileupload({dataType:"json",acceptFileTypes:/(\.|\/)(gif|jpg|jpeg|png|jpe)$/i,maxFileSize:21e4,dropZone:$("#drop"),formData:function(e){return[{name:"album_id",value:$("#albumsForUpload").val()}]},add:function(t,i){var s=$('<li><div class="alert alert-info"><img class="loader" src="public/images/ajax-loader.gif"> <a class="close" data-dismiss="alert">×</a></div></li>');s.find("div").append(i.files[0].name+" <small>[<i>"+e(i.files[0].size)+"</i>]</small>"),i.context=s.appendTo(a);var r=i.submit()},done:function(a,i){if(1==i.result.status)i.context.remove(),t.addSelectedImage(G.imageSrcPrefix+"/"+i.result.file_path),t.uploadRefreshImageList();else{i.context.empty();var s=$('<li><div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a></div></li>');s.find("div").append("<b>Error:</b> "+i.files[0].name+" <small>[<i>"+e(i.files[0].size)+"</i>]</small> "+i.result.msg),i.context.append(s)}$("#upload-msg").scrollTop(1e3)},fail:function(t,a){a.context.empty();var i=$('<li><div class="alert alert-danger"><a class="close" data-dismiss="alert">×</a></div></li>');i.find("div").append("<b>Error:</b> "+a.files[0].name+" <small>[<i>"+e(a.files[0].size)+"</i>]</small> "+a.errorThrown),a.context.append(i),$("#upload-msg").scrollTop(1e3)}}),$(document).on("drop dragover",function(e){e.preventDefault()})}};$(function(){o.init()});
